package com.op.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * ClassName： SwaggerConfigure
 * Date： 2021/10/17 20:19
 *
 * @author 孤焰
 * @version 1.0
 **/

@Configuration
@EnableSwagger2
@PropertySource(value = "classpath:swagger.yml", encoding = "utf-8")
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfigure {
    /**
     * 文档标题
     */
    @Value("${title}")
    private String title;
    /**
     * 文档描述
     */
    @Value("${description}")
    private String description;
    /**
     * 版本
     */
    @Value("${version}")
    private String version;
    /**
     * 服务条款
     */
    @Value("${termsOfServiceUrl}")
    private String termsOfServiceUrl;
    /**
     * 作者信息
     */
    private Contact DEFAULT_CONTACT = new Contact("", "", "");
    /**
     * 许可证
     */
    @Value("${license}")
    private String license;
    /**
     * 许可证链接
     */
    @Value("${licenseUrl}")
    private String licenseUrl;
    /**
     * 要扫描的包
     */
    @Value("${basePackage}")
    private String basePackage;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(title, description, version, termsOfServiceUrl, DEFAULT_CONTACT, license, licenseUrl, new ArrayList());
    }
}
